package com.boot2.controller.req;

import com.boot2.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author wzm
 * @create 2023-11-29 10:09
 * project_name:web2310
 * @Description
 */
@RestController //控制器
public class RequestController {
    //原始方式处理简单参数  httpServeletRequst对象处理(了解)
    @RequestMapping("/simple")
    public String simpleParam(HttpServletRequest request){
        //http://localhost:8080/simple?name=zs&age=18
        //获取参数
        String name = request.getParameter("name");//根据name获取参数的值
        String age = request.getParameter("age");
        //字符串转整数
        int i = Integer.parseInt(age);//手动
        System.out.println("i = " + i);

        return "ok";
    }

    //springboot接收简单参数 接受方法的形参名与传递参数名一致即可接收
    @RequestMapping("/simple01")
    public String simpl01(String name,Integer age){
        System.out.println("name+age = " + name+age);
        return "ok";
    }


    //形参与请求参数名不一致 得到的是null
   /* @RequestMapping("/simple02")
    public String simpl02(@RequestParam("name") String username, Integer age){
        System.out.println("username,age = " + username+age);
        return "ok";
    }*/

    //@Requestparam注解的requited属性默认值是true 表示参数必须传
    @RequestMapping("/simple02")
    public String simpl02(@RequestParam(name = "name",required = false) String username, Integer age){
        System.out.println("username,age = " + username+age);
        return "ok";
    }
    
    //实体参数
    @RequestMapping("/simplePojo")
    public String simplePojo(User user){
        System.out.println("user = " + user);
        return "ok";
    }

   /* @RequestMapping("/simplePojo")
    public String simplePojo(User user,@RequestParam("username")String name){
        System.out.println("user = " + user);
        user.setName(name);
        return "ok";
    }*/


    //数组请求参数
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] hobby){
        //http://localhost:8080/arrayParam?hobby=篮球&hobby=足球&hobby=音乐
        //http://localhost:8080/arrayParam?hobby=篮球,足球,乒乓球

        System.out.println("hobby = " + Arrays.toString(hobby));
        return "ok";
    }


    //集合存储多个同名参数
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> hobby){
        //http://localhost:8080/arrayParam?hobby=篮球&hobby=足球&hobby=音乐
        //http://localhost:8080/arrayParam?hobby=篮球,足球,乒乓球

        System.out.println("hobby = " + hobby);
        return "ok";
    }
}
